// bytes.js




// 格式化字节大小 (长格式,例如:"125000000 Bytes")
function FormatSizeLong(size) {
    if (size == null) { return "" }
    return size + " bytes"
}

// 格式化字节大小 (短格式，例如:"1.25GB")
function FormatSizeShort(size) {
    if (size == null) { return "" }

    let unit = 'B'
    let n = size;
    let units = ['kB', 'MB', 'GB', 'TB'];

    for (let i = 0; i < units.length; i++) {
        let next = n / 1024
        if (next < 1) {
            break
        }
        // update unit & n
        unit = units[i]
        n = next
    }

    const stringify = (num) => {
        let str = num + ""
        let strlen = str.length
        let idx = str.indexOf('.')
        if (idx < 0) { return str }
        let wantlen = idx + 2
        if (wantlen < strlen) {
            return str.substring(0, wantlen)
        }
        return str
    }

    return stringify(n) + " " + unit
}

export default {
    name: "bytes.js",
    FormatSizeLong,
    FormatSizeShort,
}
